package com.pzhu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzhu.entity.*;
import com.pzhu.service.*;
import com.pzhu.until.JwtTokenUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;
//          1">交易成功
//          2">交易失败
//          3">退款进行中
//          4>退款成功
//          5">退款失败

    /**
     * /**
     * 退款
     *
     * @param oRder
     * @return
     */

    @PostMapping("refund")
    public Result refund(@RequestBody ORder oRder) {
        if (oRder.getStatus() == 0 || oRder.getStatus() == 2 || oRder.getStatus() == 3) {
            LambdaUpdateWrapper<ORder> oRderLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            oRderLambdaUpdateWrapper.eq(ORder::getOrderId, oRder.getOrderId());
            oRderLambdaUpdateWrapper.set(ORder::getStatus, 4);
            boolean update = orderService.update(oRderLambdaUpdateWrapper);
            return new Result(Code.success, "", update);
        }
        return new Result(Code.success, "");
    }

    /**
     * 确认订单
     *
     * @param request
     * @param oRder
     * @return
     */

    @PostMapping("order")
    public Result order(HttpServletRequest request, @RequestBody ORder oRder) {
        Integer loginBusiness = JwtTokenUntil.getClaim(request, "loginBusiness").asInt();
        LambdaUpdateWrapper<ORder> oRderLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        oRderLambdaUpdateWrapper.eq(ORder::getOrderId, oRder.getOrderId());
        if (oRder.getStatus() == 0) {
            if (oRder.getType() == 1) {
                LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                goodsLambdaQueryWrapper.eq(Goods::getGoodsBusinessId, loginBusiness);
                goodsLambdaQueryWrapper.eq(Goods::getGoodsName, oRder.getGoods());
                Goods one = goodsService.getOne(goodsLambdaQueryWrapper);

                Integer stock = one.getGoodsStock();
                if (stock > oRder.getNumber()) {
                    oRderLambdaUpdateWrapper.set(ORder::getStatus, 1);
                    LambdaUpdateWrapper<Goods> goodsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                    goodsLambdaUpdateWrapper.set(Goods::getGoodsStock, one.getGoodsStock() - oRder.getNumber());
                    goodsService.update(goodsLambdaUpdateWrapper);
                } else oRderLambdaUpdateWrapper.set(ORder::getStatus, 2);
            } else oRderLambdaUpdateWrapper.set(ORder::getStatus, 1);
        }
        boolean update = orderService.update(oRderLambdaUpdateWrapper);
        return new Result(Code.success, "", update);

    }

    /**
     * 获取每年的订单数据 订单数和金额
     *
     * @param request
     * @param year
     * @return
     */
    @GetMapping("data")
    public Result getdata(HttpServletRequest request, String year) {
        Integer loginBusiness = JwtTokenUntil.getClaim(request, "loginBusiness").asInt();

        LambdaQueryWrapper<ORder> oRderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        oRderLambdaQueryWrapper.eq(ORder::getBusinessId, loginBusiness);
        oRderLambdaQueryWrapper.apply("year(order_date)={0}", year);
        List<BigDecimal> praise = new ArrayList<>(Collections.nCopies(12, BigDecimal.valueOf(0)));
        List<Integer> count = new ArrayList<>(Collections.nCopies(12, 0));


        List<ORder> list = orderService.list(oRderLambdaQueryWrapper);

        list.forEach(e -> {
            int month = e.getOrderDate().getMonth();
            count.set(month - 1, count.get(month - 1) + 1);
            praise.set(month - 1, praise.get(month - 1).add(e.getPrice().multiply(BigDecimal.valueOf(e.getNumber()))));
        });

        List<List> arrayLists = new ArrayList<>();
        arrayLists.add(praise);
        arrayLists.add(count);

        return new Result(Code.success, "", arrayLists);


    }

    /**
     * 分页查询订单条件查询未实现
     *
     * @param request
     * @param current
     * @param size
     * @param searchFrom
     * @return
     */

    @GetMapping
    public Result selest(HttpServletRequest request, @RequestParam("current") Integer current, @RequestParam("size") Integer size, SearchFrom searchFrom) {


        Integer loginBusiness = JwtTokenUntil.getClaim(request, "loginBusiness").asInt();
        LambdaQueryWrapper<ORder> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<ORder> orderPage = new Page<>(current, size);

        orderLambdaQueryWrapper.eq(ORder::getBusinessId, loginBusiness);
        if (searchFrom != null) {

//            orderLambdaQueryWrapper.like(serchFrom != null, Order::getGoods, serchFrom);
        }


        Page<ORder> page = orderService.page(orderPage, orderLambdaQueryWrapper);
        page.getRecords().forEach(e -> {

            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getUserId, e.getUserId());
            User one = userService.getOne(userLambdaQueryWrapper);

            e.setUser(one.getUserName());
        });


        return new Result(Code.success, "", page);
    }

    /**
     * 用户查询订单
     *
     * @param request
     * @param current
     * @param size
     * @return
     */
    @GetMapping("u")
    public Result selest(HttpServletRequest request, @RequestParam("current") Integer current, @RequestParam("size") Integer size) {


        Integer loginUser = JwtTokenUntil.getClaim(request, "loginUser").asInt();
//        LambdaQueryWrapper<ORder> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<ORder> orderPage = new Page<>(current, size);

//        orderLambdaQueryWrapper.eq(ORder::getUserId, loginUser);


//        Page<ORder> page = orderService.page(orderPage, orderLambdaQueryWrapper);
        Page<ORder> page = orderService.ugetOrder(loginUser, orderPage);



        return new Result(Code.success, "", page);
    }

//未使用接口
/*    @PostMapping
    public Result add(HttpServletRequest request, @RequestBody ORder order) {
        Integer loginBusiness = JwtTokenUntil.getClaim(request, "loginBusiness").asInt();

        order.setBusinessId(loginBusiness);
        boolean save = orderService.save(order);
        return new Result(Code.success, "", save);
    }*/

 /*   @DeleteMapping
    public Result delete(@RequestBody ORder order) {

        boolean b = orderService.removeById(order);
        return new Result(Code.success, "", b);

    }*/

    @PutMapping
    public Result update(@RequestBody ORder order) {

        if (order.getStatus() == 0 || order.getStatus() == 1 || order.getStatus() == 2) {
            LambdaUpdateWrapper<ORder> oRderLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            oRderLambdaUpdateWrapper.set(ORder::getStatus, 3);
            oRderLambdaUpdateWrapper.eq(ORder::getOrderId, order.getOrderId());
            boolean update = orderService.update(oRderLambdaUpdateWrapper);
            return new Result(Code.success, "");
        } else {
            return new Result(Code.success, "");
        }
    }
}
